package com.shenghui.enterprise.enterprise.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shenghui.enterprise.enterprise.entity.EntBasicInfo;
import com.shenghui.enterprise.enterprise.front.service.IFrontLoginService;
import com.shenghui.enterprise.enterprise.mapper.EntBasicInfoMapper;
import com.shenghui.enterprise.utils.GeneralUtil;
import com.shenghui.enterprise.utils.MD5;
import com.shenghui.enterprise.utils.UserUtil;
import com.shenghui.enterprise.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class FrontLoginServiceImpl implements IFrontLoginService {

    @Autowired
    private EntBasicInfoMapper entBasicInfoMapper;
    @Autowired
    private UserUtil userUtil;
    @Override
    public Result login(HttpServletRequest request, String account, String password) {
        EntBasicInfo entBasicInfo = entBasicInfoMapper.selectOne(new QueryWrapper<EntBasicInfo>().lambda().eq(EntBasicInfo::getUserAccount, account));
        if(GeneralUtil.isNotNull(entBasicInfo)){
            String pwd = entBasicInfo.getPwd();
            if(pwd.equals(MD5.MD5Encode(password))){
                userUtil.setFrontLoginUser(request,entBasicInfo);
                return Result.ok("登录成功！");
            } else {
                return Result.error("用户名或密码错误！");
            }
        } else {
            return Result.error("用户名或密码错误！");
        }
    }

    @Override
    public Result loginOut(HttpServletRequest request) {
        userUtil.frontLogout(request);
        return Result.ok("退出成功!");
    }
}
